package com.s4j.core.manager.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.Action;
import com.s4j.core.common.BaseAction;
import com.s4j.core.common.StringUtils;
import com.s4j.core.datagrid.DatagridPaging;
import com.s4j.core.manager.model.SysModule;
import com.s4j.core.manager.model.SysPerson;
import com.s4j.core.manager.service.SysPersonService;

@Controller("com.s4j.core.manager.action.SysPersonAction")
@Scope("prototype")
public class SysPersonAction extends BaseAction {

	@Resource
	private SysPersonService sysPersonService;
	
	private SysPerson person;
	
	public String mainQuery() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		Map<String, Object> condition = mixRequestCondition(request);
		
		DatagridPaging datagridPaging = sysPersonService.findByPaging(getPage(), getRows(), condition);
		
		this.setDatagridPaging(datagridPaging);
		
		return Action.SUCCESS;
	}
	
	private Map<String, Object> mixRequestCondition(HttpServletRequest request) {
		Map<String, Object> condition = new HashMap<String, Object>();
		if(person != null && !StringUtils.isNullOrBlank(person.getPersonName())) {
			condition.put("personName", person.getPersonName());
		}
		if(person != null && !StringUtils.isNullOrBlank(person.getPersonNo())) {
			condition.put("personNo", person.getPersonNo());
		}
		return condition;
	}
	
	public String person_add() {
		
		sysPersonService.insert(person);
		
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String person_edit() {
		
		sysPersonService.update(person);
		
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "修改成功");
		
		return Action.SUCCESS;
	}
	
	public String person_remove() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String personIds = request.getParameter("personIds");
		
		if(StringUtils.isNullOrBlank(personIds)) {
			this.getResultMap().put("rtnCode", Action.SUCCESS);
			this.getResultMap().put("rtnMsg", "请选择要删除的记录");
			return Action.SUCCESS;
		}
		
		sysPersonService.remove(personIds);
		
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "操作成功");
		
		return Action.SUCCESS;
	}
	
	public void init(SysModule module ,HttpServletRequest request) {
		if(!StringUtils.isNullOrBlank(request.getParameter("person.personId"))) {
			Long personId = new Long(request.getParameter("person.personId"));
			request.setAttribute("person",sysPersonService.get(personId));
		}
		String _operType = request.getParameter("_operType");
		request.setAttribute("_operType",_operType);
	}

	public SysPersonService getSysPersonService() {
		return sysPersonService;
	}

	public void setSysPersonService(SysPersonService sysPersonService) {
		this.sysPersonService = sysPersonService;
	}

	public SysPerson getPerson() {
		return person;
	}

	public void setPerson(SysPerson person) {
		this.person = person;
	}
	
}
